﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PauloC.DTO;
using PauloC.BLL;

namespace PauloC.UI.Controllers.Administracao
{
    public class UsuarioController : Controller
    {
        //
        // GET: /Usuario/
        UsuarioBLL userBLL = new UsuarioBLL();
        public ActionResult Index()
        {
            return View(userBLL.Listar());
        }

        //
        // GET: /Usuario/Details/5

        public ActionResult Details(int id)
        {
            return View(userBLL.Buscar(id));
        }

        //
        // GET: /Usuario/Create

        public ActionResult Create()
        {
            List<TipoUsuarioDTO> lisTipo = new TipoUsuarioBLL().Listar();
            List<SelectListItem> listItem = lisTipo.Select(x => new SelectListItem() 
                                                    { 
                                                        Text = x.Descricao, 
                                                        Value = x.Id.ToString() 
                                                    }).ToList();
            ViewData["listTipo"] = listItem;
            return View(new UsuarioDTO());
        }

        //
        // POST: /Usuario/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                UsuarioDTO userDTO = new UsuarioDTO();
                userDTO.DataCadastro = Convert.ToDateTime(collection["DataCadastro"]);
                userDTO.Email = collection["Email"];
                userDTO.Login = collection["Login"];
                userDTO.Nome = collection["Nome"];
                userDTO.Senha = collection["Senha"];
                userDTO.Tipo = new TipoUsuarioDTO();
                userDTO.Tipo.Id = int.Parse(collection["TipoUsuario"]);
                userDTO = userBLL.Inserir(userDTO);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Usuario/Edit/5

        public ActionResult Edit(int id)
        {
            UsuarioDTO user = userBLL.Buscar(id);
            List<TipoUsuarioDTO> lisTipo = new TipoUsuarioBLL().Listar();
            List<SelectListItem> listItem = lisTipo.Select(x => new SelectListItem()
            {
                Text = x.Descricao,
                Value = x.Id.ToString()
            }).ToList();
            listItem.First(x => x.Value == user.Tipo.Id.ToString()).Selected = true;
            ViewData["listTipo"] = listItem;
            return View(user);
        }

        //
        // POST: /Usuario/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                UsuarioDTO userDTO = userBLL.Buscar(id);
                userDTO.DataCadastro = Convert.ToDateTime(collection["DataCadastro"]);
                userDTO.Email = collection["Email"];
                userDTO.Login = collection["Login"];
                userDTO.Nome = collection["Nome"];
                userDTO.Senha = collection["Senha"];
                userDTO.Tipo = new TipoUsuarioDTO();
                userDTO.Tipo.Id = int.Parse(collection["TipoUsuario"]);
                userBLL.Atualizar(userDTO);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Usuario/Delete/5

        public ActionResult Delete(int id)
        {
            return View(userBLL.Buscar(id));
        }

        //
        // POST: /Usuario/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                userBLL.Excluir(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
